using Engine.FileFormats3D.WR2TrafficFiles;
using SharpDX;
using System;

namespace Engine.Graphics.RenderableObjects
{
    public class TrafficStreetMesh : IRenderableObject, IDisposable
    {
        #region Private Fields

        private bool hasLaneRight, hasLaneLeft;
        private Lane LaneRight, LaneLeft;
        private SplineGraphicsObject[] SplineLeft;
        private SplineGraphicsObject[] SplineRight;

        #endregion Private Fields

        #region Public Constructors

        public TrafficStreetMesh(Engine engine, Lane LaneRight, Lane LaneLeft, bool hasLaneRight, bool hasLaneLeft, Vector3[] middleLine)
        {
            this.LaneRight = LaneRight;
            this.LaneLeft = LaneLeft;
            this.hasLaneRight = hasLaneRight;
            this.hasLaneLeft = hasLaneLeft;
            SplineRight = new SplineGraphicsObject[2];
            SplineLeft = new SplineGraphicsObject[2];

            CreateGraphicobject(engine, middleLine, SplineRight, LaneRight);
            Array.Reverse(middleLine);
            CreateGraphicobject(engine, middleLine, SplineLeft, LaneLeft);
        }

        public TrafficStreetMesh()
        {
            // TODO: Complete member initialization
        }

        #endregion Public Constructors

        #region Private Destructors

        ~TrafficStreetMesh()
        {
            Dispose();
        }

        #endregion Private Destructors

        #region Public Methods

        public void Dispose()
        {
            if (this.SplineLeft != null)
                for (int i = 0; i < this.SplineLeft.Length; i++)
                {
                    if (this.SplineLeft[i] != null)
                        this.SplineLeft[i].Dispose();
                    this.SplineLeft[i] = null;
                }

            if (this.SplineRight != null)
                for (int i = 0; i < this.SplineRight.Length; i++)
                {
                    if (this.SplineRight[i] != null)
                        this.SplineRight[i].Dispose();
                    this.SplineRight[i] = null;
                }
        }

        public void Render(int renderStep)
        {
            if (this.hasLaneLeft)
            {
                this.SplineLeft[0].Render(renderStep);
                if (this.LaneLeft.numberofLanes > 1)
                    this.SplineLeft[1].Render(renderStep);
            }
            if (this.hasLaneRight)
            {
                this.SplineRight[0].Render(renderStep);
                if (this.LaneRight.numberofLanes > 1)
                    this.SplineRight[1].Render(renderStep);
            }
        }

        #endregion Public Methods

        #region Private Methods

        private void CreateGraphicobject(Engine engine, Vector3[] middleLine, SplineGraphicsObject[] Splines, Lane Lane)
        {
            //CreateMiddleLineForLane(middleLine, Lane.Offset1, out middleLineForOffset);

            Splines[0] = new SplineGraphicsObject(engine, Lane.Offset1, middleLine);

            //CreateMiddleLineForLane(middleLine, Lane.Offset2, out middleLineForOffset);

            Splines[1] = new SplineGraphicsObject(engine, Lane.Offset2, middleLine);
        }

        #endregion Private Methods
    }
}